//
//  HWHomePageHeaderView.m
//  HappyWeekend
//
//  Created by lanou on 16/1/13.
//  Copyright © 2016年 com.mjykj.www. All rights reserved.
//

#import "HWHomePageHeaderView.h"

@implementation HWHomePageHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 150)];
        _scrollView.backgroundColor = [UIColor orangeColor];
        
        _scrollView.contentSize = CGSizeMake(kScreenWidth * 4, 150);
        _scrollView.contentOffset = CGPointMake(kScreenWidth, 0);
        _scrollView.bounces = YES;
        _scrollView.alwaysBounceHorizontal = YES;
        _scrollView.alwaysBounceVertical = NO;
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
        for (int i = 0; i < 4; i++) {
            if (i == 0) {
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 150)];
                
                imageView.tag = 100 + i;
                [_scrollView addSubview:imageView];
            } else {
                UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth * i, 0, kScreenWidth, 150)];
                imageView.tag = 100 + i;
                [_scrollView addSubview:imageView];
            }
        }
        
        [self addSubview:_scrollView];
        
        self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(150, 100, 50, 50)];
        _pageControl.numberOfPages = 3;
        _pageControl.currentPage = 0;
        _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
        _pageControl.pageIndicatorTintColor = [UIColor greenColor];
        [self addSubview:_pageControl];
        
        NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timer:) userInfo:nil repeats:YES];
        [timer fire];
    }
    return self;

}


- (void)timer:(NSTimer *)timer
{
    if (_pageControl.currentPage == 2) {
        _pageControl.currentPage = 0;
        [_scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
    } else {
        _pageControl.currentPage++;
    }
    [_scrollView setContentOffset:CGPointMake((_pageControl.currentPage + 1) * kScreenWidth, 0) animated:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    _pageControl.currentPage = scrollView.contentOffset.x / kScreenWidth - 1;
    if (scrollView.contentOffset.x == 0) {
        _pageControl.currentPage = 2;
    }
    
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x < 0) {
        [scrollView setContentOffset:CGPointMake(kScreenWidth * 3, 0) animated:NO];
    } else if (scrollView.contentOffset.x > kScreenWidth * 3) {
        [scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
    }
}

- (void)setScrollViewImageArray:(NSMutableArray *)scrollViewImageArray
{
    _scrollViewImageArray = scrollViewImageArray;
    for (int i = 0; i < scrollViewImageArray.count + 1; i++) {
        UIImageView *imageView = (UIImageView *)[self viewWithTag:i + 100];
        if (i == 0) {
            HWHeaderViewModel *model = scrollViewImageArray[i + 2];
            [imageView sd_setImageWithURL:[NSURL URLWithString:model.image_url]];
        } else {
            HWHeaderViewModel *model = scrollViewImageArray[i - 1];
            [imageView sd_setImageWithURL:[NSURL URLWithString:model.image_url]];
        }
    }
}


    
    
    

@end
